DZApp.service(
				'changeOrderService',
				function(commonService) {
					return {
						query : function(postData) {
							var url = "/operationService/queryOrderInfo";
							return commonService
									.commonAjaxByPost(url, postData);
						},
						calculateOrderPrice : function(postData) {
							var url = "/operationService/calculateOrderPrice";
							return commonService
							.commonAjaxByPost(url, postData);
						},
						changeOrderAmount : function(postData) {
							var url = "/operationService/changeOrderAmount";
							return commonService
							.commonAjaxByPost(url, postData);
						},
						validate : function($scope) {
							return true;
						}
					};
				});
DZApp.controller('changeOrderController', function($scope, changeOrderService,
		$state) {
	$scope.orderInfo = {};
	$scope.orderItemlist = {};
	$scope.queryMainSeviceSelect = {};
	$scope.query = {};
	$scope.aValue = '';
	$scope.isNeedConditionValidate = false;
	var pagation ={
			prePageNum:20,
			curPageNo:1,
			curPageNo:1,
			param:null
	};
	var orderItemChangeVo  ={
			checkCode:null,
			orderNo:null,
			orderBaseDatas:null
	};
	// 翻页出发分析
	$scope.onPageChange = function() {
		pagation.curPageNo = $scope.currentPage;
		queryPagination($.param(pagation));
	};
	
	// 分页回调函数
	var doResponseCallback = function(data) {
		if ("success" == data.retCode) {
			var data  = eval(data.bizData);
			$scope.query = data.list;
			$scope.orderItemlist = data.orderItemlist;
			$scope.pageCount = data.totalPage;
		} else {
			bootboxWindow.alert('警告查询错误:' + data.errorMsg, null);
		}
	};
	// 參數封裝
	var doParam = function(){
		pagation.param = JSON.stringify($scope.orderInfo);
		return $.param(pagation);
	}
	// 分页查询
	var queryPagination = function(param){
		$scope.query = null;
	//	$('#table_1').DataTable().destroy();
		changeOrderService.query(param).then(doResponseCallback);
	}
	
	// 是否初始化的就查询
	var isQueryInit = false;
	if (isQueryInit) {
		queryPagination(doParam());
	}
	// 查詢
	$scope.queryOrderInfo = function() {
		$scope.query = null;
		queryPagination(doParam());
	}
	//查询订单项金额
	$scope.calculateOrderPrice= function(orderNo,item,obj,type){
		var orderItemChangeVo  ={
				checkCode:null,
				orderNo:null,
				orderBaseDatas:null
		};
		var val=parseFloat(obj.target.value).toFixed(2);
		if(!obj.target.value){
			bootboxWindow.alert('错误:值不能为空', null);
			return;
		}
		if( val<=0){
			if(type=="buyNum"){
				obj.target.value=item.buyNum;
				bootboxWindow.alert('错误:请输入大于0的值', null);
				
				return;
			}
		}
		if( val<0){
			bootboxWindow.alert('错误:请输入不小于0值', null);
			if(type=="buyNum"){
				obj.target.value=item.buyNum;
			}
			if(type=="unitPrice"){
				obj.target.value=item.unitPrice;
			}
			if(type=="totalPrice"){
				obj.target.value=item.totalPrice;
			}
			return;
		}
		if(type=="buyNum"){
			item.buyNum=val;
			obj.target.value=item.buyNum;
		}
		if(type=="unitPrice"){
			item.unitPrice=val;
			obj.target.value=item.unitPrice;
		}
		if(type=="totalPrice"){
			item.totalPrice=val;
			obj.target.value=item.totalPrice;
		}
		var orderBaseDatas=[];
		item.serviceNo=item.serviceSubNo;
		item.serviceProviderCode=item.serviceProvider;
		orderBaseDatas.push(item);
		orderItemChangeVo.orderNo=orderNo;
		orderItemChangeVo.orderBaseDatas=orderBaseDatas;
		var orderItem ={
				orderItemChangeVo:null
		};
		orderItem.orderItemChangeVo=angular.toJson(orderItemChangeVo);
		changeOrderService.calculateOrderPrice($.param(orderItem)).then(calculateOrderPriceCallback);
	}
	//查询金额回调
	var calculateOrderPriceCallback = function(data) {
		if ("success" == data.retCode) {
			var datas  = eval(data.bizData);
			for ( var i in $scope.orderItemlist) {
				var item =$scope.orderItemlist[i];
				for ( var j in datas) {
					var date =datas[j];
					if(date.itemId==item.itemId){
						$scope.orderItemlist[i]=date;
					}
				}
			}
			for ( var i in $scope.query) {
				var orderInfo =$scope.query[i];
				var caculatedTotalPrice=0;
				for ( var j in $scope.orderItemlist) {
					var item =$scope.orderItemlist[j];
					if(item.orderNo==orderInfo.orderNo){
						caculatedTotalPrice+=item.realTotalPrice;
					}
				}
				orderInfo.caculatedTotalPrice=caculatedTotalPrice;
			}
		} else {
			bootboxWindow.alert('警告查询错误:' + data.errorMsg, null);
		}
	}
	
	// 点击订单查看订单详情
	$scope.queryOrderItem = function($event,orderno) {
		var obj=$($event.currentTarget);
		var btn=$('#'+orderno);
		if(obj.next("tr").attr ("hidden")=="hidden"){
			obj.next("tr").attr("hidden",null);
			btn.attr("disabled", false);
		}else{
			btn.attr("disabled", true);
			obj.next("tr").attr("hidden","hidden");
		}
	}
	// 改变订单金额
	$scope.changeOrderAmount = function(orderNo) {
		var orderItemChangeVo  ={
				orderNo:null,
				orderBaseDatas:null
		};
		var orderBaseDatas=[];
		for ( var i in $scope.orderItemlist) {
			var item = $scope.orderItemlist[i];
			if (orderNo == item.orderNo) {
				item.serviceNo=item.serviceSubNo;
				item.serviceProviderCode=item.serviceProvider;
				orderBaseDatas.push(item);
			}
		}
		orderItemChangeVo.orderNo=orderNo;
		orderItemChangeVo.orderBaseDatas=orderBaseDatas;
		var orderItem ={
				orderItemChangeVo:null
		};
		orderItem.orderItemChangeVo=angular.toJson(orderItemChangeVo);
		changeOrderService.changeOrderAmount($.param(orderItem)).then(
				changeOrderAmountCallback);
	};
	var changeOrderAmountCallback=function(data){
		if ("success" == data.retCode) {
			bootboxWindow.alert('修改成功',null);
			$scope.queryOrderInfo();
		} else {
			bootboxWindow.alert('警告修改错误:' + data.errorMsg, null);
		}
	}
});
